package com.ktgj.customer.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.customer.domain.MallActivityDetails;
import com.ktgj.customer.domain.MallActivityProduct;
import com.ktgj.customer.domain.MallActivityProductDetails;
import com.ktgj.customer.domain.vo.MallActivityVO;
import com.ktgj.customer.service.IMallActivityProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.MallActivityMapper;
import com.ktgj.customer.domain.MallActivity;
import com.ktgj.customer.service.IMallActivityService;

/**
 * 活动Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-05-30
 */
@Service
public class MallActivityServiceImpl implements IMallActivityService 
{
    @Autowired
    private MallActivityMapper mallActivityMapper;
    @Autowired
    private IMallActivityProductService mallActivityProductService;

    /**
     * 查询活动
     * 
     * @param activityId 活动ID
     * @return 活动
     */
    @Override
    public MallActivity selectMallActivityById(Long activityId)
    {
        return mallActivityMapper.selectMallActivityById(activityId);
    }

    /**
     * 查询活动列表
     * 
     * @param mallActivity 活动
     * @return 活动
     */
    @Override
    public List<MallActivity> selectMallActivityList(MallActivity mallActivity)
    {
        return mallActivityMapper.selectMallActivityList(mallActivity);
    }

    /**
     * 新增活动
     * 
     * @param mallActivity 活动
     * @return 结果
     */
    @Override
    public int insertMallActivity(MallActivity mallActivity)
    {
        mallActivity.setCreateTime(DateUtils.getNowDate());
        return mallActivityMapper.insertMallActivity(mallActivity);
    }

    /**
     * 修改活动
     * 
     * @param mallActivity 活动
     * @return 结果
     */
    @Override
    public int updateMallActivity(MallActivity mallActivity)
    {
        mallActivity.setUpdateTime(DateUtils.getNowDate());
        return mallActivityMapper.updateMallActivity(mallActivity);
    }

    /**
     * 批量删除活动
     * 
     * @param activityIds 需要删除的活动ID
     * @return 结果
     */
    @Override
    public int deleteMallActivityByIds(Long[] activityIds)
    {
        return mallActivityMapper.deleteMallActivityByIds(activityIds);
    }

    /**
     * 删除活动信息
     * 
     * @param activityId 活动ID
     * @return 结果
     */
    @Override
    public int deleteMallActivityById(Long activityId)
    {
        return mallActivityMapper.deleteMallActivityById(activityId);
    }

    //后台活动列表
    @Override
    public List<MallActivityVO> activityList(Long typeId, String activityName) {
        List<MallActivityVO> mallActivityVOS = mallActivityMapper.activityList(typeId, activityName);
        if (mallActivityVOS != null && mallActivityVOS.size() > 0) {
            mallActivityVOS.forEach(item -> {
                if (item.getTypeId().equals("10")) {
                    item.setTypeId("团购");
                } else if (item.getTypeId().equals("20")) {
                    item.setTypeId("秒杀");
                } else if (item.getTypeId().equals("30")) {
                    item.setTypeId("组合商品");
                } else if (item.getTypeId().equals("40")) {
                    item.setTypeId("权益兑换");
                }
                if (item.getIfpass().equals("0")) {
                    item.setIfpass("未审");
                } else {
                    item.setIfpass("已审");
                }
            });
        }
        return mallActivityVOS;
    }

    //后台活动详情
    @Override
    public MallActivityDetails activityDetails(Long activityId) {
        MallActivityDetails activityDetails = mallActivityMapper.activityDetails(activityId);
        List<MallActivityProductDetails> detailsList = mallActivityProductService.mallActivityProducts(activityId);
        activityDetails.getMallActivityProducts().addAll(detailsList);
        return activityDetails;
    }

    //后台修改活动详情
    @Override
    public int updateActivityDetails(MallActivityDetails activityDetails) {
        MallActivityDetails details = activityDetails(activityDetails.getActivityId());
        MallActivity activity = new MallActivity();
        BeanUtils.copyProperties(activityDetails, activity);
        if (details.getMallActivityProducts().size() == activityDetails.getMallActivityProducts().size()) {
            activityDetails.getMallActivityProducts().forEach(item -> {
                MallActivityProduct activityProduct = new MallActivityProduct();
                activityProduct.setActivityProductId(item.getActivityProductId());
                activityProduct.setSalePrice(item.getSalePrices());
                activityProduct.setStockCount(item.getStockCounts());
                activityProduct.setSort(item.getSort());
                activityProduct.setUpdateTime(new Date());
                mallActivityProductService.updateMallActivityProduct(activityProduct);
            });
        } else if (details.getMallActivityProducts().size() < activityDetails.getMallActivityProducts().size()) {
            activityDetails.getMallActivityProducts().forEach(item -> {
                MallActivityProduct activityProduct = new MallActivityProduct();
                if (item.getActivityProductId() == 0) {
                    activityProduct.setActivityId(activityDetails.getActivityId());
                    activityProduct.setProductId(item.getProductId());
                    activityProduct.setProductSpecId(item.getProductSpecId());
                    activityProduct.setSort(item.getSort());
                    activityProduct.setSalePrice(item.getSalePrices());
                    activityProduct.setStockCount(item.getStockCounts());
                    activityProduct.setCreateTime(new Date());
                    activityProduct.setUpdateTime(new Date());
                    mallActivityProductService.insertMallActivityProduct(activityProduct);
                } else {
                    activityProduct.setActivityProductId(item.getActivityProductId());
                    activityProduct.setSalePrice(item.getSalePrices());
                    activityProduct.setStockCount(item.getStockCounts());
                    activityProduct.setSort(item.getSort());
                    activityProduct.setUpdateTime(new Date());
                    mallActivityProductService.updateMallActivityProduct(activityProduct);
                }
            });
        } else if (details.getMallActivityProducts().size() > activityDetails.getMallActivityProducts().size()) {
            List<Long> detailsLong = new ArrayList<>();
            List<Long> activityLong = new ArrayList<>();
            details.getMallActivityProducts().forEach(item -> {
                Long id = item.getActivityProductId();
                detailsLong.add(id);
            });
            activityDetails.getMallActivityProducts().forEach(item -> {
                MallActivityProduct activityProduct = new MallActivityProduct();
                Long id = item.getActivityProductId();
                activityLong.add(id);
                activityProduct.setActivityProductId(item.getActivityProductId());
                activityProduct.setSalePrice(item.getSalePrices());
                activityProduct.setStockCount(item.getStockCounts());
                activityProduct.setSort(item.getSort());
                activityProduct.setUpdateTime(new Date());
                mallActivityProductService.updateMallActivityProduct(activityProduct);
            });
            Collection collection = new ArrayList(detailsLong);
            collection.removeAll(activityLong);
            collection.forEach(item -> {
                MallActivityProduct activityProduct = new MallActivityProduct();
                activityProduct.setActivityProductId((Long) item);
                activityProduct.setDeleteTime(new Date());
                mallActivityProductService.updateMallActivityProduct(activityProduct);
            });
        }
        return updateMallActivity(activity);
    }

    //后台添加活动
    @Override
    public int addActivityDetails(MallActivityDetails activityDetails) {
        MallActivity activity = new MallActivity();
        BeanUtils.copyProperties(activityDetails, activity);
        int mallActivity = insertMallActivity(activity);
        if (activityDetails.getMallActivityProducts().size() > 0) {
            activityDetails.getMallActivityProducts().forEach(item -> {
                MallActivityProduct activityProduct = new MallActivityProduct();
                activityProduct.setActivityId(activity.getActivityId());
                activityProduct.setProductId(item.getProductId());
                activityProduct.setProductSpecId(item.getProductSpecId());
                activityProduct.setSort(item.getSort());
                activityProduct.setSalePrice(item.getSalePrices());
                activityProduct.setStockCount(item.getStockCounts());
                activityProduct.setCreateTime(new Date());
                activityProduct.setUpdateTime(new Date());
                mallActivityProductService.insertMallActivityProduct(activityProduct);
            });
        }
        return mallActivity;
    }

    //后台删除活动
    @Override
    public int deleteActivityDetails(Long activityId) {
        MallActivityDetails details = activityDetails(activityId);
        MallActivity activity = new MallActivity();
        activity.setActivityId(activityId);
        activity.setDeleteTime(new Date());
        if (details.getMallActivityProducts() != null && details.getMallActivityProducts().size() > 0) {
            details.getMallActivityProducts().forEach(item -> {
                MallActivityProduct activityProduct = new MallActivityProduct();
                activityProduct.setActivityProductId(details.getActivityId());
                activityProduct.setDeleteTime(new Date());
                mallActivityProductService.updateMallActivityProduct(activityProduct);
            });
        }
        return updateMallActivity(activity);
    }
}
